package com.acme.bankapp.domain.bank;

import com.acme.exceptions.*;

public class CheckingAccount extends AbstractAccount {
  private double overdraft;
  
  public CheckingAccount(double balance, double overdraft) throws 
                         IllegalArgumentException{
    if(balance <0.0 || overdraft <0.0){
      throw new IllegalArgumentException();
    }
    this.balance = balance;
    this.overdraft = overdraft;
  }
  @Override
  public void deposit(double x) throws IllegalArgumentException {
    if(x<0.0){
      throw new IllegalArgumentException();
    }
    balance +=x;
  }

  @Override
  public void withdraw(double x) throws IllegalArgumentException, 
                       NotEnoughFundsException{
    if(x <0.0){
      throw new IllegalArgumentException();
    }
    if(x>balance + overdraft){
      throw new OverDraftLimitExceededException(this.balance, this.overdraft);
    }
    if(x > this.balance){
      overdraft -= (x-balance);
      balance = 0;
    }
  }
  
  @Override
  public double maximumAmountToWithdraw(){
    return balance + overdraft;
  }
  
  @Override
  public double getBalance(){
    return this.balance;
  }
  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    long temp;
    temp = Double.doubleToLongBits(balance);
    result = prime * result + (int) (temp ^ (temp >>> 32));
    temp = Double.doubleToLongBits(overdraft);
    result = prime * result + (int) (temp ^ (temp >>> 32));
    return result;
  }
  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    CheckingAccount other = (CheckingAccount) obj;
    if (Double.doubleToLongBits(balance) != Double
        .doubleToLongBits(other.balance))
      return false;
    if (Double.doubleToLongBits(overdraft) != Double
        .doubleToLongBits(other.overdraft))
      return false;
    return true;
  }
  
  @Override
  public String toString() {
    return "CheckingAccount [balance=" + balance + ", overdraft=" + overdraft
        + "]";
  }
  
}
